﻿using ProjScan.RecordManager.AssetData;
using ProjScan.ScanRule;
using ProjScan.Tools;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEngine;

namespace ProjScan.RecordManager
{
    internal class MeshGeometryRecordManager : RecordManagerBase<MeshGeometryAssetData>
    {
        public override string TableTag => "mesh";

        public override void PrepareAssets()
        {
            datas = new List<MeshGeometryAssetData>();
            string[] source = new string[4]
            {
                "fbx",
                "asset",
                "obj",
                "mesh"
            };
            int num = AssetPathManager.Instance.artPathList.Length;
            for (int i = 0; i < num; i++)
            {
                string text = AssetPathManager.Instance.artPathList[i];
                string value = Path.GetExtension(text).ToLower().Replace(".", "");
                if (!source.Contains(value))
                {
                    continue;
                }

                ActiveAllRecord();
                if (!EnableRule(text))
                {
                    continue;
                }

                ModelImporter modelImporter = AssetImporter.GetAtPath(text) as ModelImporter;
                if (modelImporter != null)
                {
                    Object[] array = AssetDatabase.LoadAllAssetsAtPath(text);
                    if (array == null)
                    {
                        continue;
                    }

                    Object[] array2 = array;
                    foreach (Object @object in array2)
                    {
                        Mesh mesh = @object as Mesh;
                        if (mesh != null)
                        {
                            int iD = AssetPathManager.Instance.GetID(Path.GetDirectoryName(text));
                            string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(text);
                            MeshGeometryAssetData meshGeometryAssetData = new MeshGeometryAssetData(iD, text, fileNameWithoutExtension, mesh, modelImporter);
                            meshGeometryAssetData.id = datas.Count;
                            CheckAssets(meshGeometryAssetData);
                            meshGeometryAssetData.Reset();
                            datas.Add(meshGeometryAssetData);
                            if (datas.Count % 100 == 0)
                            {
                                ToolsHelper.GC();
                            }
                        }
                    }

                    continue;
                }

                Object[] array3 = AssetDatabase.LoadAllAssetsAtPath(text);
                if (array3 == null)
                {
                    continue;
                }

                if (array3.Length == 1)
                {
                    Mesh mesh2 = array3[0] as Mesh;
                    if (!(mesh2 != null))
                    {
                        continue;
                    }

                    MeshGeometryAssetData meshGeometryAssetData2 = new MeshGeometryAssetData(AssetPathManager.Instance.GetID(Path.GetDirectoryName(text)), text, mesh2, null);
                    meshGeometryAssetData2.id = datas.Count;
                    foreach (RecordRule recordRule in recordRules)
                    {
                        if (dicEnableRules[recordRule])
                        {
                            recordRule.Check(meshGeometryAssetData2);
                        }
                    }

                    meshGeometryAssetData2.Reset();
                    datas.Add(meshGeometryAssetData2);
                    if (datas.Count % 100 == 0)
                    {
                        ToolsHelper.GC();
                    }

                    continue;
                }

                Object[] array4 = array3;
                foreach (Object object2 in array4)
                {
                    Mesh mesh3 = object2 as Mesh;
                    if (!(mesh3 != null))
                    {
                        continue;
                    }

                    int iD2 = AssetPathManager.Instance.GetID(Path.GetDirectoryName(text));
                    string fileNameWithoutExtension2 = Path.GetFileNameWithoutExtension(text);
                    MeshGeometryAssetData meshGeometryAssetData3 = new MeshGeometryAssetData(iD2, text, fileNameWithoutExtension2, mesh3, modelImporter);
                    meshGeometryAssetData3.id = datas.Count;
                    foreach (RecordRule recordRule2 in recordRules)
                    {
                        if (dicEnableRules[recordRule2])
                        {
                            recordRule2.Check(meshGeometryAssetData3);
                        }
                    }

                    meshGeometryAssetData3.Reset();
                    datas.Add(meshGeometryAssetData3);
                    if (datas.Count % 100 == 0)
                    {
                        ToolsHelper.GC();
                    }
                }
            }
        }
    }
}